home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15217 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  41 lines

  1. Path: news.production.compuserve.com!news
  2. From: Peter Hammond <100443.2074@CompuServe.COM>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: returning an array from function
  5. Date: 4 Apr 1996 08:11:20 GMT
  6. Organization: Cygnus Intruments Ltd
  7. Message-ID: <4k0078$15m$2@mhadf.production.compuserve.com>
  8. References: <4jtr5h$h2s@nadine.teleport.com>
  9.  
  10. I am getting really ((*$%รบ off with this post editor. Sometimes I 
  11. can put in newlines, sometimes I cant.
  12. Anyway, another method which might work is:
  13.  
  14. const int MaxN = 80;
  15. void ACaller (Void) {
  16.     ...
  17.     int N= // 1 to MAxN
  18.     int* array = MakeMyArray (N);
  19.     DoSomething (array[i]);
  20. }
  21.  
  22. int* MakeMyArray (int N) {
  23.     static int array[MaxN];
  24.     // OR: 
  25.     int* array = new int[N];
  26.     ...
  27.     return array;
  28. }
  29.  
  30. If MakeMyArray uses new, then ACaller MUST call delete (not 
  31. necessarily directly) to get rid of it. This being C++, you could 
  32. probably do something elegant with constructors/destructors. 
  33. Otherwise, the array must be static so it is guaranteed to still 
  34. exist after returning its pointer. I think.
  35.  
  36. -- 
  37. Cygnus Instruments Ltd        Ultrasonic NDT Equipment
  38.    30 Prince of Wales Rd, Dorchester, Dorset DT1 1PW            
  39.    England    
  40. Tel +44 (0)1305 265533        Fax +44 (0)1305 269960
  41.